package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@RestController
@RequestMapping("ydd_advpos")
@Slf4j
public class YddAdvposController {
    @Autowired
    YddAdvposService yddAdvposService;
    @Autowired
    YddAdvimgService yddAdvimgService;

    //广告位列表分页
    //@Cacheable(value = "ydd_advpos", key = "yddadvpos",condition = "#page != null")
    @GetMapping("/list")
    public Result page(String advposdesc, String advposname, int page, int limit) {
        //分页构造器
        Page<YddAdvpos> pageInfo = new Page<>(page, limit);
        Page<YddAdvposDto> dtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(advposdesc != null, YddAdvpos::getAdvposdesc, advposdesc);
        lqw.like(advposname != null, YddAdvpos::getAdvposname, advposname);
        //排序
        lqw.orderByDesc(YddAdvpos::getId);
        //调用service
        Page<YddAdvpos> page1 = yddAdvposService.page(pageInfo, lqw);

        //拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<YddAdvpos> records = page1.getRecords();
        List<YddAdvposDto> list = records.stream().map(item -> {
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            Integer id = item.getId();

            LambdaQueryWrapper<YddAdvimg> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(YddAdvimg::getAdvimgpos, id);
            int advposnum = yddAdvimgService.count(lqw2);
            BeanUtils.copyProperties(item, yddAdvposDto);
            yddAdvposDto.setAdvposnum(advposnum);
            return yddAdvposDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);

        return pageInfo != null ? Result.ok("", PageResult.answer(dtoPage.getTotal(), dtoPage.getSize(), dtoPage.getCurrent(), dtoPage.getRecords())) : Result.error("暂无标签数据");

    }

    //广告位回显
    @GetMapping("/rowInfo")
    public Result selectById(Long id) {
        log.info("广告位回显 id:{}", id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return Result.ok("", yddAdvpos);
    }

    //广告位修改
    //@CacheEvict(value = "ydd_advpos", allEntries = true)
    @PostMapping("/update")
    public Result update(@RequestBody YddAdvpos yddAdvpos) {
        log.info("广告位修改 advimg:{}", yddAdvpos);
        yddAdvposService.updateById(yddAdvpos);
        return Result.ok("", yddAdvpos);
    }

    //广告位删除
    //@CacheEvict(value = "ydd_advpos", allEntries = true)
    @GetMapping("/del")
    public Result delete(long id) {
        log.info("广告位删除 id:{}", id);
        yddAdvposService.removeById(id);
        return Result.ok("", id);
    }

    //广告位添加
    //@CacheEvict(value = "ydd_advpos", allEntries = true)
    @PostMapping("/add")
    public Result add(@RequestBody YddAdvpos yddAdvpos) {
        log.info("广告位添加 advimg:{}", yddAdvpos);
        yddAdvposService.save(yddAdvpos);
        return Result.ok("", yddAdvpos);
    }

    //广告位全部 ?
    @GetMapping("/all")
    public Result all() {
        log.info("广告位全部");
        List<YddAdvpos> list = yddAdvposService.list();
        return Result.ok("", list);
    }


}
